import http from "@/utils/http";
import {
	GW
} from "@/common/const";

const href = window.location.href;
const matcha = href.match(/stock=\w{2}(\w+)/);
window._stockCode = matcha ? matcha[1] : ""; //股票代码
console.log('window._stockCode', window._stockCode)
window._stockName = ""; //股票名称

/*
 * Get请求示例
 */
//公司新闻
export const corpNew = async option => {
	const url = GW + "/gw/ynstream/stream/info/client/company/new/list";
	const params = {
		stockCode: window._stockCode,
		pageNum: 1,
		pageSize: 10,
		...option,
	};
	params.pageNum -= 1;
	const res = await http.get(url, params);
	return res;
};
//行业新闻
export const getNew = async option => {
	const url = GW + "/gw/ynstream/stream/info/client/industry/new/list";
	const params = {
		stockCode: window._stockCode,
		pageNum: 1,
		pageSize: 10,
		...option,
	};
	params.pageNum -= 1;

	const res = await http.get(url, params);
	return res;
};
//公司公告
export const corpNotice = async option => {
	const url = GW + "/gw/ynstream/api/client/company/notice/f10/list";
	const params = {
		stockCode: window._stockCode,
		pageNum: 1,
		pageSize: 10,
		...option,
	};
	params.pageNum -= 1;
	const res = await http.get(url, params);
	return res;
};
//经营分析
export const getBusiness = async option => {
	const url = GW + "/gw/ynstockinfo/client/business";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//股东研究
// export const getHolder = async option => {
//     const url = GW + "/gw/ynstockinfo/client/stock/holder";
//     const params = {
//         stockCode: window._stockCode,
//         forceFlush:true,
//         ...option,
//     };
//     const res = await http.get(url, params);
//     return res.data;
// };

//股东研究
export const getHolder = async option => {
	const url = GW + "/gw/ynstockinfo/client/newStockHolder";
	const params = {
		stockCode: window._stockCode,
		forceFlush: false,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//股本结构
export const getStructure = async option => {
	const url = GW + "/gw/ynstockinfo/client/stock/structure";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//分红融资
export const profit = async option => {
	const url = GW + "/gw/ynstockinfo/client/stock/profit";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//研究报告
export const report = async option => {
	const url = GW + "/gw/ynstream/api/client/report/f10/list";
	const params = {
		stockCode: window._stockCode,
		pageNum: 0,
		pageSize: 10,
		...option,
	};
	const res = await http.get(url, params);
	return res;
};
//行业研报
export const tradereport = async option => {
	const url = GW + "/gw/ynstream/api/client/industry/report/list";
	const params = {
		stockCode: window._stockCode,
		pageNum: 1,
		pageSize: 10,
		...option,
	};
	const res = await http.get(url, params);
	return res;
};

//资本运作
export const fund = async option => {
	const url = GW + "/gw/ynstockinfo/client/info/fund";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//盈利预测
export const infoprofit = async option => {
	const url = GW + "/gw/ynstockinfo/client/info/profit";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//公司高管---高管列表
export const mngInfo = async option => {
	const url = GW + "/gw/ynstockinfo/client/info/mngInfo";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//公司高管----高管持股变动
export const mngChg = async option => {
	const url = GW + "/gw/ynstockinfo/client/info/mngChg";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//财务分析
export const finance = async option => {
	const url = GW + "/gw/ynstockinfo/client/finance";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//财务分析----判断股票类型
export const stockType = async option => {
	const url = GW + "/gw/ynstockinfo/client/stockType";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//核心题材
export const coreTheme = async option => {
	const url = GW + "/gw/ynstockinfo/client/coreTheme";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//同行比较
export const comparison = async option => {
	const url = GW + "/gw/ynstockinfo/client/comparison";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//关联个股
export const relevance = async option => {
	const url = GW + "/gw/ynstockinfo/client/relevance";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//公司大事-大事提醒&重大事项
export const eventEvent = async option => {
	const url = GW + "/gw/ynstockinfo/client/event";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//公司大事-限售解禁&十大股东持股变动&高管持股变动
export const eventHolder = async option => {
	const url = GW + "/gw/ynstockinfo/client/holder";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//公司大事-限售解禁&十大股东持股变动&高管持股变动
export const eventExcrChange = async option => {
	const url = GW + "/gw/ynstockinfo/client/excrChange";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//操盘必读-大事提醒&公司概况&最新指标
export const guideProfile = async option => {
	const url = GW + "/gw/ynstockinfo/client/guide/profile";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//操盘必读-股东分析&核心题材&龙虎榜
export const guideHolder = async option => {
	const url = GW + "/gw/ynstockinfo/client/guide/holder";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//操盘必读-公司新闻&公司公告
export const guideNews = async option => {
	const url = GW + "/gw/ynstockinfo/client/guide/news";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

//股票
export const stock = async option => {
	const url = "http://app.yueniucj.com/app/clickSearchContent.htm";
	const params = {
		searchType: 1,
		content: window._stockCode,
		...option,
	};
	const res = await http.get(url, params);
	return res.data;
};

// 三位分节法----如果是小数点两位     / 10000
export const number = (ops, num, wan = 10000) => {
	if (!ops && ops != 0) {
		return "--";
	}

	const res = ops % 1 == 0 ? ops : ops.toFixed(2); //如果是小数点两位
	let value = num ? Number((ops / wan).toFixed(2)).toLocaleString() : ""; //三位分节法
	const integer = ops % 1 == 0 ? Number(res).toLocaleString() + ".00" : ""; //三位分节法的整数
	if (num) {
		const len = value.split(".")[1];
		value = len && len.length < 2 ? value + "0" : value;
		value = (ops / wan).toFixed(2) % 1 == 0 ? value + ".00" : value;
	}

	if (ops == 0) {
		return 0;
	}
	return num ? value : integer || res;
};


// 三位分节法----如果是小数点两位     / 10000
export const changeNumber = (ops, num) => {
	if (!ops) {
		return 0;
	}
	if (ops == 0) {
		return 0;
	}
	let data = ops / num
	// const res = data % 1 == 0 ? data : ops.toFixed(2); //如果是小数点两位
	const res = data.toFixed(2)
	return res;
};
// 三位分节法----如果是小数点两位   不/ 10000
export const numberNo = (ops, num) => {
	if (!ops) return "--";
	ops = Number(ops);
	const res = ops % 1 == 0 ? ops : ops.toFixed(2); //如果是小数点两位
	let value = num ? Number(ops.toFixed(2)).toLocaleString() : ""; //三位分节法
	const integer = ops % 1 == 0 ? Number(res).toLocaleString() + ".00" : ""; //三位分节法的整数

	if (num) {
		const len = value.split(".")[1];
		value = len && len.length < 2 ? value + "0" : value;
		value = ops.toFixed(2) % 1 == 0 ? value + ".00" : value;
	}

	if (ops == 0) {
		return 0;
	}

	return num ? value : integer || res;
};

export const numAddUnit = (num) => {
	var changeNum = "--"
	if (num >= 100000000) {
		changeNum = formatDecimal(num / 100000000) + "亿"
	} else if (num >= 10000 && num < 100000000) {
		changeNum = formatDecimal(num / 10000) + "万"
	} else if (num >= 0 && num < 10000) {
		changeNum = num
	}
	return changeNum
}
// 三位分节法----如果是小数点两位     / 10000
export const f10changeNum = (num) => {
	let numUnit = "--"
	if (num >= 0) {
		numUnit = numAddUnit(num)
	} else {
		numUnit = '-' + numAddUnit(Math.abs(num))
	}

	return numUnit
};

/**
 * 保留小数点 不能四舍五入
 * @param num
 * @param decimal
 * @returns {string}
 */
function formatDecimal(num, decimal = 2) {
	num = num.toString()
	let index = num.indexOf('.')
	if (index !== -1) {
		num = num.substring(0, decimal + index + 1)
	} else {
		num = num.substring(0)
	}
	return parseFloat(num).toFixed(decimal)
}


//公司概况
export const profile = async option => {
	const url = GW + "/gw/ynstockinfo/client/profile";
	const params = {
		stockCode: window._stockCode,
		...option,
	};
	const res = await http.post(url, params);
	window._stockName = res.data.profile.aStockName;
	return res.data;
};


// http://gw-dev.zx093.com/gw/ynstockinfo/client/fin?stockCode=600028&type=index&interval=report&size=8'
/**
 * 财务数据
 * @param stockCode              股票代码
 * @param type                   类型 index-指标，cash-现金流量，income-利润，bala-负债
 * @param interval               时间区间 report-报告期，year-年报，quarter-季度
 * @param size                   日期倒序前n条  默认8条
 * @returns {Promise<AxiosResponse<any>>}
 */
export const finList = ({
	stockCode = '',
	type = 'index',
	interval = 'report',
	size = 8
}) => {

	const params = {
		stockCode,
		type,
		interval,
		size
	};

	return http.get(`${GW}/gw/ynstockinfo/client/fin`, params)
};

/**
 * 查询股票简单信息
 * @param code
 * @returns {*}
 * http://gw.zx093.com/gw/ynstockinfo/app/queryStock?code=688001
 */
export const queryStock = (code = '') => {
	return http.get(`${GW}/gw/ynstockinfo/app/queryStock`, {
		code: window._stockCode
	});
};